<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
class UserIntegralController extends Controller
{
    public function show(Request $request){
       $integral = $request->user()->userIntegral->integral;
       $status = [
           "integral"=>$integral,
       ];
        return response()->json($status)->setStatusCode(200);
    }

    public function show_detail(Request $request){
        //可用积分
        $sum_integral = $request->user()->userIntegral->integral;
        $start_time = Carbon::now()->startOfMonth()->toDateTimeString();
        $end_time = Carbon::now()->endOfMonth()->toDateTimeString();
        $add_integral = $request->user()->userIntegralLogs()
                        ->whereBetween("created_at",[$start_time,$end_time])
                        ->where("integral",">",0)
                        ->sum("integral");
        $dec_integral = $request->user()->userIntegralLogs()
                        ->whereBetween("created_at",[$start_time,$end_time])
                        ->where("integral","<",0)
                        ->sum("integral");
        $data = [
            "sum_integral"=>$sum_integral,//总积分数
            "add_integral"=>$add_integral,//本月新增积分
            "dec_integral"=>$dec_integral,//本月消耗积分
             "sign_in_day"=>$request->user()->sin_in_day,//签到天数
        ];
//        dump($start_time);
        return response()->json($data)->setStatusCode(200);
//        dd($request->user()->sin_in_day);
    }
}
